package com.example.hospital.api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateRange;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.example.hospital.api.common.R;
import com.example.hospital.api.controller.form.DeptWorkPlanForm;
import com.example.hospital.api.service.MedicalDeptWorkPlanService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/medical/dept/workPlan")
public class MedicalDeptWorkPlanController {

    @Resource
    private MedicalDeptWorkPlanService medicalDeptWorkPlanService;

    @PostMapping("/select")
//    @SaCheckLogin
//    @SaCheckPermission(value = {"ROOT", "SCHEDULE:SELECT"}, mode = SaMode.OR)
    public R selectByPage(@RequestBody @Valid DeptWorkPlanForm form) {
        Map param = BeanUtil.beanToMap(form);
        //根据开始和结束日期生成连续的日期
        DateRange range = DateUtil.range(new DateTime(form.getStartDate()),
                new DateTime(form.getEndDate()), DateField.DAY_OF_MONTH);

        List<String> dateList = new ArrayList<>();
        range.forEach(e->{
            dateList.add(e.toDateStr());
        });
        List<Map> mapList = medicalDeptWorkPlanService.selectWorkPlanInRange(param, dateList);
        dateList.clear();
        range.reset();
        //王dateList放入格式化后的日期，用于表头输出
        range.forEach(e->{
            dateList.add(e.toString("MM月dd日") + "（" + e.dayOfWeekEnum().toChinese() + "）");
        });

        return R.ok().put("result", mapList).put("dateList", dateList);
    }

}
